﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Partial Public Class NodeView

#Region " Dependant Properties "

    Public Shared ReadOnly ProjectProperty As DependencyProperty = DependencyProperty.Register("Project", GetType(Project), GetType(NodeView), New UIPropertyMetadata(AddressOf NodeView.ProjectPropertyChanged))

    Public Property Project() As Project
        Get
            Return CType(GetValue(ProjectProperty), Project)
        End Get
        Set(ByVal value As Project)
            SetValue(ProjectProperty, value)
        End Set
    End Property


    Private Shared Sub ProjectPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim NodeView As NodeView = CType(d, Controls.NodeView)
        ' When Changed Update something
    End Sub

#End Region

    Public Sub Refresh(ByVal Project As Project, ByVal Node As Node)
        Me.DataContext = Node
        ux_GridSubNodes.Children.Clear()
        '----------------------------
        Dim r As New RowDefinition
        r.Height = New GridLength(0, GridUnitType.Auto)
        ux_GridSubNodes.RowDefinitions.Add(r)
        For count As Integer = 0 To Node.ChildNodes.Count - 1
            Dim c As New ColumnDefinition
            c.Width = New GridLength(Node.ChildNodes(count).ComputeWidthInNodes * 305)
            ux_GridSubNodes.ColumnDefinitions.Add(c)
            Dim x As New NodeView
            x.Width = Node.ChildNodes(count).ComputeWidthInNodes * 305
            x.Refresh(Project, Node.ChildNodes(count))
            ux_GridSubNodes.Children.Add(x)
            x.Height = Me.Height - 100 'TODO: Check values and hight of Datagrid.
            Grid.SetColumn(x, count)
        Next
        '------------------------------------------------------
        'Me.ux_WorkItems.Children.Clear()
        Dim qry As New System.Text.StringBuilder
        qry.Append("SELECT [System.Id], [System.Title] ")
        qry.Append("FROM WorkItems ")
        qry.Append("WHERE ")
        qry.Append("(")
        qry.Append("[System.WorkItemType] = 'Change Request'  ")
        qry.Append("OR  [System.WorkItemType] = 'Requirement'")
        qry.Append(")  ")
        If Node.IsIterationNode Then
            qry.Append("AND  [System.IterationID] = '" & Node.Id & "' ")
        Else
            qry.Append("AND  [System.AreaID] = '" & Node.Id & "' ")
        End If
        qry.Append("ORDER BY [System.Id]")

        Dim qryrslt As WorkItemCollection = Project.Store.Query(qry.ToString)
        ' ux_ControlListTest.DataContext = qryrslt
        ux_ControlListTest.ItemsSource = qryrslt
        'For Each wi As WorkItem In qryrslt
        '    Dim newSIC As New WitView
        '    newSIC.DataContext = wi
        '    Me.ux_WorkItems.Children.Add(newSIC)
        'Next
    End Sub

End Class
